let merit = 0;
const woodfish = document.getElementById('woodfish');
const hammer = document.getElementById('hammer');
const meritCounter = document.getElementById('meritCounter');

// 图片数组 - 这里需要替换成实际的图片路径
const images = [
    'images/current_image.jpg',
    // 添加更多图片...
];

const temptationBtn = document.getElementById('temptationBtn');
const imagePopup = document.getElementById('imagePopup');
const randomImage = document.getElementById('randomImage');
const loadingIndicator = document.getElementById('loadingIndicator');
const doomMessage = document.getElementById('doomMessage');

let isLoading = false;

// 添加一个变量来跟踪图片是否被点击
let isImageClicked = false;
let hideImageTimeout = null;
let isDoomShowing = false;

woodfish.addEventListener('click', function(e) {
    // 增加功德
    merit++;
    meritCounter.textContent = `功德: ${merit}`;
    
    // 播放敲击动画
    hammer.classList.remove('active');
    void hammer.offsetWidth; // 触发重排
    hammer.classList.add('active');
    
    // 创建浮动的+1文字
    const floatingMerit = document.createElement('div');
    floatingMerit.className = 'floating-merit';
    floatingMerit.textContent = '+1';
    
    // 修改初始位置：将文字位置往上移动100px
    const rect = woodfish.getBoundingClientRect();
    floatingMerit.style.left = `${rect.left + rect.width/2}px`;
    floatingMerit.style.top = `${rect.top - 80}px`; // 起始位置就在木鱼上方
    
    document.body.appendChild(floatingMerit);
    
    // 动画结束后移除元素
    setTimeout(() => {
        document.body.removeChild(floatingMerit);
    }, 1000);
});

// 添加图片点击事件
randomImage.addEventListener('click', function() {
    isImageClicked = true;
    // 清除可能存在的隐藏定时器
    if (hideImageTimeout) {
        clearTimeout(hideImageTimeout);
        hideImageTimeout = null;
    }
    // 显示心形图标
    heartContainer.style.display = 'block';
});

// 心形按钮点击事件
heartBtn.addEventListener('click', function() {
    // 显示通知
    notification.classList.add('show');
    
    // 添加弹跳动画
    heartBtn.style.transform = 'scale(1.3)';
    setTimeout(() => {
        heartBtn.style.transform = 'scale(1)';
    }, 200);
    
    // 3秒后隐藏通知
    setTimeout(() => {
        notification.classList.remove('show');
    }, 3000);
});

temptationBtn.addEventListener('click', async function(e) {
    // 如果有显示的图片，先隐藏它和心形图标
    if (imagePopup.style.display === 'block') {
        randomImage.classList.remove('show');
        imagePopup.style.display = 'none';
        heartContainer.style.display = 'none'; // 隐藏心形图标
        notification.classList.remove('show'); // 隐藏通知
        isImageClicked = false;
    }
    
    // 减少功德
    merit--;
    meritCounter.textContent = `功德: ${merit}`;
    
    // 检查功德是否为负数
    if (merit < 0 && !isDoomShowing) {
        showDoomMessage();
    }
    
    // 显示-1的动画
    const floatingNegative = document.createElement('div');
    floatingNegative.className = 'floating-merit negative';
    floatingNegative.textContent = '-1';
    
    const rect = temptationBtn.getBoundingClientRect();
    floatingNegative.style.left = `${rect.left + rect.width/2}px`;
    floatingNegative.style.top = `${rect.top - 20}px`;
    
    document.body.appendChild(floatingNegative);
    
    setTimeout(() => {
        document.body.removeChild(floatingNegative);
    }, 1000);
    
    try {
        // 调用API获取新图片
        const response = await fetch('/get_and_save_image');
        const data = await response.json();
        
        if (data.status === 'success') {
            // 更新图片src
            randomImage.src = data.image_path;
            
            // 显示图片
            imagePopup.style.display = 'block';
            isImageClicked = false; // 重置点击状态
            
            setTimeout(() => {
                randomImage.classList.add('show');
            }, 10);
            
            // 只有在图片未被点击时才设置自动隐藏
            hideImageTimeout = setTimeout(() => {
                if (!isImageClicked) {
                    randomImage.classList.remove('show');
                    setTimeout(() => {
                        imagePopup.style.display = 'none';
                    }, 300);
                }
            }, 3000);
            
        } else {
            console.error('Failed to get image:', data.message);
        }
    } catch (error) {
        console.error('Error fetching image:', error);
    }
});

// 添加加载提示
randomImage.onloadstart = function() {
    // 显示加载中提示
    console.log('图片加载中...');
};

randomImage.onerror = function() {
    // 处理图片加载失败
    console.error('图片加载失败');
};

// 显示加载提示
loadingIndicator.style.display = 'block';
// 隐藏加载提示
loadingIndicator.style.display = 'none';

// 添加显示"你完蛋了"的函数
function showDoomMessage() {
    isDoomShowing = true;
    doomMessage.style.display = 'flex';
    
    // 播放音效（可选）
    const audio = new Audio('doom-sound.mp3'); // 需要准备音效文件
    audio.play().catch(e => console.log('Audio play failed:', e));
    
    // 添加点击事件来关闭消息
    doomMessage.addEventListener('click', function hideMessage() {
        doomMessage.style.display = 'none';
        isDoomShowing = false;
        doomMessage.removeEventListener('click', hideMessage);
    }, { once: true });
    
    // 3秒后自动消失
    setTimeout(() => {
        if (isDoomShowing) {
            doomMessage.style.display = 'none';
            isDoomShowing = false;
        }
    }, 3000);
}

function addScreenShake() {
    document.body.style.animation = 'shake 0.5s ease-in-out';
    setTimeout(() => {
        document.body.style.animation = '';
    }, 500);
} 